#!/bin/sh /etc/rc.common
START=50

run_socatg()
{
	local v6port
	local v4host
	local v4port

	config_get v6port "$1" v6port
	config_get v4host "$1" v4host
	config_get v4port "$1" v4port

	# Check if all required parameters are provided
	if [ -n "$v6port" ] && [ -n "$v4host" ] && [ -n "$v4port" ]; then
		socat TCP6-LISTEN:$v6port,reuseaddr,fork TCP4:$v4host:$v4port &
		echo "Started socat forwarding: IPv6 port $v6port -> $v4host:$v4port"
	fi
}

start()
{
	echo "Starting SocatG service..."
	config_load socatg
	config_foreach run_socatg forward
}

stop()
{
	echo "Stopping SocatG service..."
	killall socat 2>/dev/null
	echo "SocatG service stopped."
}

restart()
{
	stop
	sleep 2
	start
}